package com.aduerr.v1shouyin.mvp.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2018/10/24 16:02
 * @change
 * @chang time
 * @class describe
 */

public class PageNumSettingActivity extends AppCompatActivity {

    private final String[] m = {"1张", "2张"};
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_page)
    TextView tvPage;
    @BindView(R.id.sp_page)
    Spinner spPage;
    @BindView(R.id.cb_print)
    CheckBox cbPrint;
    @BindView(R.id.cb_auto_print)
    CheckBox cbAutoPrint;
    @BindView(R.id.rl_page_num)
    RelativeLayout rlPageNum;
    private ArrayAdapter<String> adapter;
    private boolean isFirst = true;//由于SpinnerSelectedListener 初始化会触发事件，所以加个标志位

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_num_setting);
        ButterKnife.bind(this);
        tvTitle.setText("小票设置");

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spPage.setAdapter(adapter);

        //添加事件Spinner事件监听
        spPage.setOnItemSelectedListener(new SpinnerSelectedListener());

        //设置默认值
        spPage.setVisibility(View.VISIBLE);

        cbPrint.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked) {
                int printpage =
                        (int) SharedPreferencesUtils.getParam(PageNumSettingActivity.this, "printpage", 1);

                if(printpage == 0) printpage = 1;

                SharedPreferencesUtils.setParam(PageNumSettingActivity.this, "printpage"
                        , printpage);
                tvPage.setText(printpage + "张");
                rlPageNum.setVisibility(View.VISIBLE);
            } else {
                SharedPreferencesUtils.setParam(PageNumSettingActivity.this, "printpage"
                        , 0);
                rlPageNum.setVisibility(View.GONE);
            }
        });

        cbAutoPrint.setChecked((boolean) SharedPreferencesUtils.getParam(PageNumSettingActivity.this, "autoprint", false));

        cbAutoPrint.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked) {
                SharedPreferencesUtils.setParam(PageNumSettingActivity.this, "autoprint"
                        , true);
            } else {
                SharedPreferencesUtils.setParam(PageNumSettingActivity.this, "autoprint"
                        , false);
            }
        });

        Log.i("TAG",
                "小票设置 0==" + SharedPreferencesUtils.getParam(PageNumSettingActivity.this, "printpage", -1));

        int printpage =
                (int) SharedPreferencesUtils.getParam(PageNumSettingActivity.this,
                        "printpage", 1);
        if(printpage == 0) {
            cbPrint.setChecked(false);
            tvPage.setText("0张");
            rlPageNum.setVisibility(View.GONE);
        } else {
            cbPrint.setChecked(true);
            tvPage.setText(printpage + "张");
            rlPageNum.setVisibility(View.VISIBLE);
            spPage.setSelection(printpage - 1);
        }


    }

    @OnClick({R.id.img_back})
    void clicked(View v) {
        switch(v.getId()) {
            case R.id.img_back:
                finish();
                break;
        }

    }

    //使用数组形式操作
    class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {

            Log.i("TAG",
                    "小票设置 1==" + SharedPreferencesUtils.getParam(PageNumSettingActivity.this, "printpage", -1));

            if(isFirst) {
                isFirst = false;
                return;
            }

            tvPage.setText(m[arg2]);
            SharedPreferencesUtils.setParam(PageNumSettingActivity.this, "printpage",
                    arg2 + 1);
            Log.i("TAG",
                    "小票设置 2==" + SharedPreferencesUtils.getParam(PageNumSettingActivity.this, "printpage", -1));
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}
